﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ServerLib.Email
{
    public class EmailHelper
    {
        public static string RemoveSigns(string inString)
        {
            return inString.Replace("<", string.Empty).Replace(">", string.Empty);
        }

        /*****************************************************
    '* BreakAddressLine()							     *
    '* Passes:		char *inAddress,					 *
    '*				char *name,							 *
    '*				int namelen,						 *
    '*				char *addr,							 *
    '*				int addrlen							 *
    '* Returns:		n/a									 *
    '* Description:	This routine is used to parse thru	 *
    '*				a From: header line and retrieve the *
    '*				email addr and user name if any from *
    '*				it. There are several variations of  *
    '*				the from line (stupid) so we have to *
    '*				be able to handle them all			 *
    '*****************************************************/
        public static void BreakAddressLine(string inAddress, ref string Name, ref string Address)
        {
            int z = 0;
            bool bCommentOn = false;
            string workBuffer = string.Empty, lineBuffer = string.Empty;

            // make a copy for our work buffer 
            workBuffer = inAddress.Trim();
            /* Possible Email address formats are as follows:
        	To: name@domain.com
        	To: John Doe <name@domain.com>
        	To: "Doe, John Q." <name@domain.com>
        	To: name@domain.com (The big guy)
        	To: John. (The Eliminator) Nerk@domain.com
        	To: "description" <dfdfd>

        	1. All comments (...) are removed
        	2. Check for " " if they exist, they wrap around our description
        	3. If <> exist, then an address resides inside them and everything else is descriptive
        	4. If no <> and no spaces, then the address is all that is left with no description
            */
            // 1) go and parse out comments
            for(int i = 0; i < workBuffer.Length; i++)
            {
                if(workBuffer[i] == '(' && !bCommentOn)
                {
                    if(i > 0 && workBuffer[i - 1] != '/')
                        bCommentOn = true;
                }
                else if(workBuffer[i] == ')' && bCommentOn)
                {
                    if(i > 0 && workBuffer[i - 1] != '/')
                        bCommentOn = false;
                }
            
                // add to line buf 'cause we aint in a comment 
                if(!bCommentOn && workBuffer[i] != ')')
                    lineBuffer += workBuffer[i];
            }
        
            // copy our comment-free string over to work buffer 
            workBuffer = lineBuffer.Trim();

            // 2) go and parse through looking for description "..." if quotes exist
            if(workBuffer.IndexOf('"') > -1)
            {
                for(int q = 0; q < workBuffer.Length; q++)
                {
                    if(workBuffer[q] == '"')
                    {
                        if(workBuffer[q - 1] != '/')
                            bCommentOn = !bCommentOn;
                    }
                    else
                    {
                        if(bCommentOn) // put this stuff into our name buffer
                            Name += workBuffer[q];
                        else // add to line buf cause we aint in a comment
                            lineBuffer += workBuffer[q];
                    }
                }
            }
            // copy our description-free string over to work buffer 
            workBuffer = lineBuffer.Trim();

            // 3) If < > exist, then extract the email address from them
            int iStart = workBuffer.IndexOf("<");
            int iEnd = workBuffer.LastIndexOf(">");
            if(iStart > -1 && iEnd > -1)
            {
                if(workBuffer[iStart - 1] != '/' && workBuffer[iEnd - 1] != '/')
                {
                    for(z = iStart; z <= iEnd; z++)
                    {
                        if(workBuffer[z] != '<' && workBuffer[z] != '>')
                            Address += workBuffer[z];
                    }
                }
            }
            else
            {
                // check to see if we have an address with a domain in here
                z = workBuffer.IndexOf("@");
                if(z > 0)
                {
                    iStart = workBuffer.IndexOf(" ", z);
                    if(iStart == -1)
                        iStart = 0;
                    iEnd = workBuffer.IndexOf(" ", z);
                    if(iEnd == -1)
                        iEnd = workBuffer.Length;

                    Address = workBuffer.Substring(iStart, iEnd - iStart);
                }
                else
                {
                    // no address with a domain so just assume whatever is left is an address
                    Address = workBuffer;
                }
            }

        }
    

    //    public DateTime ConvertUnixDate(inDate
    //Public Function ConvertUnixDateString(ByVal inDateString As String) As Date
    //    Dim sParts() As String
    //    Dim sBuf As String = "", sDay As String = "", sMonth As String = "", sYear As String = "", sTime As String = "", sZone As String = ""

    //    sParts = Split(inDateString, " ")
    //    ' Date format is as follows:
    //    '   Fri, 28 Mar 2003 22:53:01 -0500
    //    If UBound(sParts) > 0 Then
    //        ' sParts(0) is ingnored (dont care about day name)
    //        sDay = sParts(1)
    //        Select Case UCase(sParts(2))
    //            Case "JAN" : sMonth = "01"
    //            Case "FEB" : sMonth = "02"
    //            Case "MAR" : sMonth = "03"
    //            Case "APR" : sMonth = "04"
    //            Case "MAY" : sMonth = "05"
    //            Case "JUN" : sMonth = "06"
    //            Case "JUL" : sMonth = "07"
    //            Case "AUG" : sMonth = "08"
    //            Case "SEP" : sMonth = "09"
    //            Case "OCT" : sMonth = "10"
    //            Case "NOV" : sMonth = "11"
    //            Case "DEC" : sMonth = "12"
    //        End Select
    //        sYear = sParts(3)
    //        sTime = sParts(4)
    //        sZone = sParts(5)
    //        sBuf = sMonth + "/" + sDay + "/" + sYear + " " + sTime
    //    End If
    //    Return CDate(sBuf)
    //End Function

    }
}
